import 'package:flutter/material.dart';

import '../common.dart';

class SimpleTextInputDialog extends StatefulWidget {
  /// return null if text taken, or else return a String to display as the error text
  final String Function(String) onSubmit;
  final String title;
  final String hintText;

  SimpleTextInputDialog(this.onSubmit, this.title, this.hintText);

  static show(BuildContext context, Function(String) onSubmit, String title,
      String hintText) {
    showDialog(
        context: context,
        child: Dialog(child: SimpleTextInputDialog(onSubmit, title, hintText)));
  }

  @override
  State<StatefulWidget> createState() => _SimpleTextInputState();
}

class _SimpleTextInputState extends State<SimpleTextInputDialog> {
  TextEditingController controller;
  String errorText;

  @override
  void initState() {
    super.initState();
    controller = TextEditingController();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: 200,
      child: Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Container(
              padding: EdgeInsetsDirectional.only(top: 20, start: 20),
              child: Text(
                widget.title,
                textScaleFactor: 1,
                style: TextStyle(color: TColor.textBlack, fontSize: 16),
              )),
          Container(
            alignment: AlignmentDirectional.centerStart,
            margin: EdgeInsetsDirectional.only(
              start: 20,
              end: 20,
              top: 20,
            ),
            child: TextField(
              maxLines: 1,
              textAlignVertical: TextAlignVertical.center,
              style: TextStyle(fontSize: 14, color: TColor.textBlack),
              decoration: InputDecoration(
                border: OutlineInputBorder(
                    borderSide: BorderSide(color: TColor.dividerGray)),
                hintText: widget.hintText,
                hintStyle: TextStyle(color: TColor.textHint, fontSize: 14),
                focusedBorder: OutlineInputBorder(
                    borderSide: BorderSide(color: TColor.primeColor)),
                errorText: errorText,
                errorStyle: TextStyle(fontSize: 12, color: TColor.textError),
                errorBorder: OutlineInputBorder(
                    borderSide: BorderSide(color: TColor.textError)),
              ),
              controller: controller,
              onChanged: (text) {
                if (TUtil.isNotEmptyString(errorText)) {
                  setState(() {
                    errorText = null;
                  });
                }
              },
            ),
          ),
//          Visibility(
//            visible: TUtil.isNotEmptyString(errorText),
//            child: Container(
//              margin: EdgeInsetsDirectional.only(start: 20, ),
//              child: Text(errorText??'', style: TextStyle(fontSize: 12, color: TColor.textError),),
//            ),
//          ),
          SizedBox(
            height: 20,
          ),
          Divider(
            thickness: 1,
            height: 1,
            color: TColor.dividerGray,
          ),
          Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              Expanded(
                flex: 1,
                child: GestureDetector(
                  behavior: HitTestBehavior.opaque,
                  onTap: () => Navigator.maybePop(context),
                  child: Container(
                    alignment: Alignment.center,
                    height: 45,
                    width: double.infinity,
                    child: Text(
                      AppLocalizations.of(context).cancel(),
                      textScaleFactor: 1,
                      style: TextStyle(fontSize: 14, color: TColor.textHint),
                    ),
                  ),
                ),
              ),
              Container(
                height: 45,
                width: 1,
                color: TColor.dividerGray,
              ),
              Expanded(
                flex: 1,
                child: GestureDetector(
                  behavior: HitTestBehavior.opaque,
                  onTap: () {
                    final errString = widget.onSubmit(controller.text);
                    if (errString == null) {
                      Navigator.maybePop(context);
                    } else {
                      setState(() {
                        errorText = errString;
                      });
                    }
                  },
                  child: Container(
                    alignment: Alignment.center,
                    height: 45,
                    width: double.infinity,
                    child: Text(
                      AppLocalizations.of(context).OK(),
                      textScaleFactor: 1,
                      style:
                          TextStyle(fontSize: 14, color: TColor.primeColor),
                    ),
                  ),
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }
}
